<?php
// $Id: table_dish.php 429 2012-06-03 01:47:50Z tomqin@gmail.com $

class Table_Dish_Module{
    public $user = null;

    public function __construct($user){
        $this->user = $user;
    }

    public function check_user_right($shop_id){
        if($this->user->status == USER_ADMIN){
            return true;
        }elseif($this->user->status == USER_BUSINESS){
            $uid = $this->user->uid;
            $shop_id = intval($shop_id);
            return Db::getFirst("SELECT count(shop_id) FROM `biz_shop` WHERE admin_uid = '$uid' AND shop_id = '$shop_id'");
        }else{
            return false;
        }
    }

    public function get_data($shop_id, $dish_id){
        $shop_id = intval($shop_id);
        $dish_id = intval($dish_id);
        return Db::getRow("SELECT * FROM `biz_dish` WHERE `shop_id` = '{$shop_id}' AND `dish_id` = '{$dish_id}'");
    }

    public function get_list($shop_id, array $dish_cats){
        $shop_id = intval($shop_id);
        $stmt = Db::query("SELECT * FROM `biz_dish` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC");
        while($row = $stmt->fetch_assoc()){
            if(isset($dish_cats[$row['dish_cat']])){
                $list[$row['dish_cat']][] = $row;
            }else{
                $list[0][] = $row;
            }
        }
        return $list;
    }

    public function get_list_cat($shop_id){
        $shop_id = intval($shop_id);
        return Db::getKeyArray("SELECT * FROM `biz_dish_cat` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC", 'dish_cat');
    }

    public function get_array_cat($shop_id){
        $shop_id = intval($shop_id);
        $res = Db::getKeyArray("SELECT dish_cat, cat_name FROM `biz_dish_cat` WHERE `shop_id` = '{$shop_id}' ORDER BY ordnum DESC", 'dish_cat', 'cat_name');
        return $res ? $res : array();
    }

    public function insert(array $post){
        return true;
    }

    public function update($shop_id, $dish_id, array $post){
        $dish_id = intval($dish_id);
        $shop_id = intval($shop_id);
        $post = $this->dish_fields($post);
        Db::update('biz_dish', $post, array('dish_id' => $dish_id, 'shop_id' => $shop_id));
        return true;
    }

    public function import($shop_id, array $post){
        $dish_cat  = $post['dish_cat'];
        $stmt = Db::prepare('INSERT INTO `biz_dish` (shop_id, dish_cat, name, picture, description, price, ordnum, status) VALUES (?,?,?,?,?,?,0,1)');
        $stmt->bind_param('iisssd', $shop_id, $dish_cat, $name, $picture, $description, $price);
        foreach($post['name'] as $key => $picture){
            $name        = $post['name'][$key];
            $picture     = '';
            $description = $post['description'][$key];
            $price       = $post['price'][$key];
            if($name !='' && $price !=''){
                $stmt->execute();
            }
        }
        return true;
    }

    public function delete($shop_id, $dish_id){
        $shop_id = intval($shop_id);
        $dish_id = intval($dish_id);
        Db::query("DELETE FROM `biz_dish` WHERE shop_id = '{$shop_id}' AND dish_id = '{$dish_id}'");
        return true;
    }

    public function sort($shop_id, array $array){
        $stmt = Db::prepare('UPDATE `biz_dish` SET ordnum = ? WHERE dish_id = ? AND shop_id = ?');
        $stmt->bind_param('iii', $ordnum, $dish_id, $shop_id);
        foreach($array as $dish_id => $ordnum){
            $stmt->execute();
        }
    }

    protected function dish_fields($post){
        return array(
            'shop_id'    => array_get_int($post, 'shop_id'),
            'name'       => array_get_string($post, 'name'),
            'dish_cat'   => array_get_int($post, 'dish_cat'),
            'status'     => array_get_int($post, 'status'),
            'subname'    => array_get_string($post, 'subname'),
            'description'=> array_get_string($post, 'description'),
            'picture'    => array_get_string($post, 'picture'),
            'price'      => array_get_double($post, 'price'),
            'ordnum'     => array_get_int($post, 'ordnum'),
        );
    }
}
?>